/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.SysNotify;
import com.thinkgem.jeesite.modules.sys.entity.SysUser;
import com.thinkgem.jeesite.modules.sys.service.SysNotifyService;
import com.thinkgem.jeesite.modules.test.entity.TestDataMain;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通知通告Controller
 *
 * @author kongxiangsheng
 * @version 2014-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysNotify")
public class SysNotifyController extends BaseController {
    @Autowired
    private SysNotifyService sysNotifyService;

    @RequiresPermissions("sys:sysNotify:view")
    @RequestMapping(value = {"list", ""})
    public String list(SysNotify sysNotify, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<SysNotify> page = new BasePage<SysNotify>(request, response);
        sysNotifyService.selectNotices(page, sysNotify);
        model.addAttribute("page", page);
        return "modules/sys/SysNotify/list";
    }

    @RequiresPermissions("sys:sysNotify:view")
    @RequestMapping(value = "form")
    public String form(SysNotify sysNotify, Model model) {
        if (StringUtils.isNotBlank(sysNotify.getId())) {
            sysNotify = sysNotifyService.getWithChildren(sysNotify.getId());
        }
        model.addAttribute("sysNotify", sysNotify);
        return "modules/sys/SysNotify/form";
    }

    @RequiresPermissions("sys:sysNotify:edit")
    @RequestMapping(value = "save")
    public String save(SysNotify sysNotify, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, sysNotify)) {
            return form(sysNotify, model);
        }
        // 如果是修改，则状态为已发布，则不能再进行操作
        if (StringUtils.isNotBlank(sysNotify.getId())) {
            SysNotify e = sysNotifyService.getWithNums(sysNotify.getId());
            if ("1".equals(e.getStatus())) {
                addMessage(redirectAttributes, "已发布，不能操作！");
                return "redirect:" + adminPath + "/sys/sysNotify/form?id=" + sysNotify.getId();
            }
        }
        sysNotifyService.saveWithChildren(sysNotify);
        addMessage(redirectAttributes, "保存通知'" + sysNotify.getTitle() + "'成功");
        return "redirect:" + adminPath + "/sys/sysNotify/?repage";
    }

    @RequiresPermissions("sys:sysNotify:edit")
    @RequestMapping(value = "delete")
    public String delete(SysNotify sysNotify, RedirectAttributes redirectAttributes) {
        sysNotifyService.deleteWithChildren(sysNotify);
        addMessage(redirectAttributes, "删除通知成功");
        return "redirect:" + adminPath + "/sys/sysNotify/?repage";
    }

    /**
     * 我的通知列表
     */
    @RequestMapping(value = "self")
    public String selfList(SysNotify sysNotify, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<SysNotify> page = new BasePage<SysNotify>(request, response);
        sysNotifyService.selectMyNotices(page);
        model.addAttribute("page", page);
        return "modules/sys/SysNotify/selfList";
    }

    /**
     * 我的通知列表-数据
     */
    @RequiresPermissions("sys:sysNotify:view")
    @RequestMapping(value = "selfData")
    @ResponseBody
    public Page<SysNotify> listData(SysNotify sysNotify, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<SysNotify> page = new BasePage<SysNotify>(request, response);
        sysNotifyService.selectMyNotices(page);
        return page;
    }

    /**
     * 查看我的通知
     */
    @RequestMapping(value = "view")
    public String view(SysNotify sysNotify, Model model) {
        if (StringUtils.isNotBlank(sysNotify.getId())) {
            sysNotifyService.updateReadFlag(sysNotify.getId());
            sysNotify = sysNotifyService.getWithChildren(sysNotify.getId());
            model.addAttribute("sysNotify", sysNotify);
            return "modules/sys/SysNotify/form";
        }
        return "redirect:" + adminPath + "/sys/sysNotify/self?repage";
    }

    /**
     * 查看我的通知-数据
     */
    @RequestMapping(value = "viewData")
    @ResponseBody
    public SysNotify viewData(SysNotify sysNotify, Model model) {
        if (StringUtils.isNotBlank(sysNotify.getId())) {
            sysNotifyService.updateReadFlag(sysNotify.getId());
            return sysNotify;
        }
        return null;
    }

    /**
     * 查看我的通知-发送记录
     */
    @RequestMapping(value = "viewRecordData")
    @ResponseBody
    public SysNotify viewRecordData(SysNotify sysNotify, Model model) {
        if (StringUtils.isNotBlank(sysNotify.getId())) {
            sysNotify = sysNotifyService.getWithChildren(sysNotify.getId());
            return sysNotify;
        }
        return null;
    }

    /**
     * 获取我的通知数目
     */
    @RequestMapping(value = "self/count")
    @ResponseBody
    public String selfCount(Model model) {
        return String.valueOf(sysNotifyService.selectMyNoticeNum());
    }
}